<template>
  <div class="login-container">
    <audio controls="controls" class="songs" autoplay="autoplay">
        <source src="src\views\login\赵海洋 - 《瞬间的永恒》夜色钢琴曲.mp3" type="audio/mpeg"/>
    </audio>
    <el-form class="login-form" autoComplete="on" :model="loginForm" :rules="loginRules" ref="loginForm" label-position="left">
      <div class="title-container">
        <h3 class="title">系统登录</h3>
      </div>
      <el-form-item prop="name">
        <el-input name="name" type="text" v-model="loginForm.name" autoComplete="on" placeholder="用户名">
          <span slot="prefix" class="svg-container"><svg-icon icon-class="user" /></span>
        </el-input>
      </el-form-item>

      <el-form-item prop="password">
        <el-input name="password" :type="passwordType" @keyup.enter.native="handleLogin" v-model="loginForm.password"
          autoComplete="on" placeholder="密码">
          <span class="svg-container" slot="prefix"><svg-icon icon-class="password" /></span>
        </el-input>
        <span class="show-pwd" @click="showPwd">
          <svg-icon icon-class="eye" />
        </span>
      </el-form-item>

      <el-button type="primary" style="width:100%;margin-bottom:30px;" :loading="loading" @click.native.prevent="handleLogin">登录</el-button>
    </el-form>
    
  </div>
</template>

<script>
import { isvalidUsername } from '@/utils/validate'

export default {
  name: 'login',
  data() {
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码至少6位以上'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        name: '',
        password: ''
      },
      loginRules: {
        name: [{ required: true, trigger: 'blur', message: '用户名不能为空'}],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      passwordType: 'password',
      loading: false,
      showDialog: false
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('LoginByUsername', this.loginForm)
          .then((response) => {
            this.loading = false

            this.$router.push({ path: '/' })
          }).catch((error) => {
            this.$message({
              message: error.message,
              type: 'error',
              duration: 3 * 1000
            });

            this.loading = false
          })
        } else {
          return false
        }
      });
    },
  },

  created() {
  },
  destroyed() {
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;
$cursor: #fff;
$background: url(\assets\background.gif) cover no-repeat;

.login-container {
  position: fixed;
  height: 100%;
  width: 100%;
  // background-color: $bg;

  .login-form {
    border-radius: 8px;
    border: 1px solid #eaeaea;
    box-shadow: 0 0px 8px 0 rgba(0, 0, 0, 0.06), 0 1px 0px 0 rgba(0, 0, 0, 0.02);
    position: absolute;
    left: 0;
    right: 0;
    width: 450px;
    padding: 35px 35px 15px 35px;
    margin: 80px auto;
  }

  .svg-container {
    color: $dark_gray;
    vertical-align: middle;
    width: 25px;
    display: inline-block;
  }

  .title-container {
    position: relative;
    .title {
      font-size: 26px;
      color: $dark_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 0px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
